/**
 * 
 */
package com.zxc.curator.nodes;

import java.util.ArrayList;
import java.util.List;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.nodes.GroupMember;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.CloseableUtils;

/**
 * 集团成员（服务器在线状态监测）
 * @author Administrator
 *
 */
public class GroupMemberExample {

	private static final int QTY = 5;
    private static final String PATH = "/examples/group/member";
    
	public static void main(String[] args) {
		
		List<GroupMember> list = new ArrayList<GroupMember>();
		
		 CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.18.100:2181", new ExponentialBackoffRetry(1000, 3));
       
		 try {
			 client.start();
	         for (int i = 0; i < QTY; ++i) {
	        	 GroupMember member = new GroupMember(client, PATH, "member#"+i);
	        	 member.setThisData("abc=123".getBytes());
	        	 member.start();
	        	 list.add(member);
	         }
	         Thread.sleep(2000);
	         list.get(0).close();
	         Thread.sleep(10000);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			for (GroupMember groupMember : list) {
				try {
					CloseableUtils.closeQuietly(groupMember);
				} catch (Exception e2) {
				}
			}
			CloseableUtils.closeQuietly(client);
		}
        
         
	}
}
